import datetime

from apps.data_stats.data_stats.clean import BaseClean


class GDPClean(BaseClean):
    def deal_df_one(self, df_one, name_mapping):
        item = df_one[["tag_name", "num"]].set_index("tag_name").T.rename(columns=name_mapping).to_dict("index")["num"]
        item.update({"time": df_one["date_string"].tolist()[0]})
        if "type" in df_one:
            item.update({"type": df_one["type"].tolist()[0]})
        return item

    def run_xj(self):
        df = self.get_data_from_db(
            cate_1="国民经济核算",
            cate_2="国内生产总值(现价)",
            condition=" and area = '全国' and data_type = '季度数据'",
        )
        df = df[df["date_string"].str.startswith(tuple(str(i) for i in range(1992, datetime.datetime.now().year + 1)))]
        df["type"] = df["tag_name"].map(lambda x: x.split("_")[1] + "（亿元）")
        df["tag_name"] = df["tag_name"].map(lambda x: x.split("_")[0])
        name_mapping = {
            "国内生产总值": "gross_domestic_product",
            "第一产业增加值": "value_added_of_primary_industry",
            "第二产业增加值": "value_added_of_secondary_industry",
            "第三产业增加值": "value_added_of_tertiary_industry",
            "农林牧渔业增加值": "value_added_of_agriculture_forestry_animal_husbandry_and_fishery",
            "工业增加值": "value_added_of_industry",
            "制造业增加值": "value_added_of_manufacturing_industry",
            "建筑业增加值": "value_added_of_construction_industry",
            "批发和零售业增加值": "value_added_of_wholesale_and_retai_industry",
            "交通运输、仓储和邮政业增加值": "value_added_of_transportation_industry",
            "住宿和餐饮业增加值": "value_added_of_accommodation_and_catering_industry",
            "金融业增加值": "value_added_of_financial_industry",
            "房地产业增加值": "value_added_of_real_estate_industry",
            "信息传输、软件和信息技术服务业增加值": "value_added_of_it_industry",
            "租赁和商务服务业增加值": "value_added_of_leasing_and_business_services_industry",
            "其他行业增加值": "value_added_of_other_industries",
        }
        res = df.groupby(["date_string", "type"]).apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "gdp_current_price")

    def save_data(self, datas, table="gdp_current_price"):
        batch_size = 1000
        for i in range(0, len(datas), batch_size):
            self.to_db.add_batch_smart(table, list(datas[i:i + batch_size]), update_columns=list(datas[0].keys()))

    def run_byj(self):
        df = self.get_data_from_db(
            cate_1="国民经济核算",
            cate_2="国内生产总值(不变价)",
            condition=" and area = '全国' and data_type = '季度数据'",
        )
        df = df[df["date_string"].str.startswith(tuple(str(i) for i in range(2007, datetime.datetime.now().year + 1)))]
        df["type"] = df["tag_name"].map(lambda x: x.split("_")[1] + "（亿元）")
        df["tag_name"] = df["tag_name"].map(lambda x: x.split("_")[0])
        name_mapping = {
            "国内生产总值(不变价)": "gross_domestic_product",
            "第一产业增加值(不变价)": "value_added_of_primary_industry",
            "第二产业增加值(不变价)": "value_added_of_secondary_industry",
            "第三产业增加值(不变价)": "value_added_of_tertiary_industry",
        }
        res = df.groupby(["date_string", "type"]).apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "gdp_constant_price")

    def run_zs(self):
        df = self.get_data_from_db(
            cate_1="国民经济核算", cate_2="国内生产总值指数", condition=" and area = '全国' and data_type = '季度数据'"
        )
        df = df[df["date_string"].str.startswith(tuple(str(i) for i in range(1993, datetime.datetime.now().year + 1)))]
        df["type"] = df["tag_name"].map(lambda x: x.split("_")[1])
        df["tag_name"] = df["tag_name"].map(lambda x: x.split("_")[0])
        name_mapping = {
            "国内生产总值指数(上年同期=100)": "gross_domestic_product_index",
            "第一产业增加值指数(上年同期=100)": "value_added_of_primary_industry_index",
            "第二产业增加值指数(上年同期=100)": "value_added_of_secondary_industry_index",
            "第三产业增加值指数(上年同期=100)": "value_added_of_tertiary_industry_index",
            "农林牧渔业增加值指数(上年同期=100)": "value_added_of_agriculture_forestry_animal_husbandry_and_fishery_index",
            "工业增加值指数(上年同期=100)": "value_added_of_industry_index",
            "制造业增加值指数(上年同期=100)": "value_added_of_manufacturing_industry_index",
            "建筑业增加值指数(上年同期=100)": "value_added_of_construction_industry_index",
            "批发和零售业增加值指数(上年同期=100)": "value_added_of_wholesale_and_retai_industry_index",
            "交通运输、仓储和邮政业增加值指数(上年同期=100)": "value_added_of_transportation_industry_index",
            "住宿和餐饮业增加值指数(上年同期=100)": "value_added_of_accommodation_and_catering_industry_index",
            "金融业增加值指数(上年同期=100)": "value_added_of_financial_industry_index",
            "房地产业增加值指数(上年同期=100)": "value_added_of_real_estate_industry_index",
            "信息传输、软件和信息技术服务业增加值指数(上年同期=100)": "value_added_of_it_industry_index",
            "租赁和商务服务业增加值指数(上年同期=100)": "value_added_of_leasing_and_business_services_industry_index",
            "其他行业增加值指数(上年同期=100)": "value_added_of_other_industries_index",
        }
        res = df.groupby(["date_string", "type"]).apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "gdp_index")

    def run_hb(self):
        df = self.get_data_from_db(
            cate_1="国民经济核算",
            cate_2="国内生产总值环比增长速度",
            condition=" and area = '全国' and data_type = '季度数据'",
        )
        df = df[df["date_string"].str.startswith(tuple(str(i) for i in range(2011, datetime.datetime.now().year + 1)))]
        name_mapping = {
            "国内生产总值环比增长速度": "gdp_mom_growth_rate",
        }
        res = df.groupby(["date_string"]).apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "gdp_mom_growth_rate")

    def run_zsgxl(self):
        df = self.get_data_from_db(
            cate_1="国民经济核算",
            cate_2="三大需求对国内生产总值增长的贡献率",
            condition=" and area = '全国' and data_type = '季度数据'",
        )
        df = df[df["date_string"].str.startswith(tuple(str(i) for i in range(2016, datetime.datetime.now().year + 1)))]
        df["type"] = df["tag_name"].map(lambda x: x.split("_")[1] + "(%)")
        df["tag_name"] = df["tag_name"].map(lambda x: x.split("_")[0])
        name_mapping = {
            "最终消费支出对国内生产总值增长贡献率": "final_consumption_expenditure_gdp_growth_contribution_rate",
            "资本形成总额对国内生产总值增长贡献率": "total_investment_gdp_growth_contribution_rate",
            "货物和服务净出口对国内生产总值增长贡献率": "net_exports_of_goods_and_services_gdp_growth_contribution_rate",
        }
        res = df.groupby(["date_string", "type"]).apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "three_major_demands_gdp_growth_contribution_rate")

    def run_gxl(self):
        df = self.get_data_from_db(
            cate_1="国民经济核算", cate_2="三次产业贡献率", condition=" and area = '全国' and data_type = '季度数据'"
        )
        df = df[df["date_string"].str.startswith(tuple(str(i) for i in range(2016, datetime.datetime.now().year + 1)))]
        df["type"] = df["tag_name"].map(lambda x: x.split("_")[1] + "(%)")
        df["tag_name"] = df["tag_name"].map(lambda x: x.split("_")[0])
        name_mapping = {
            "国内生产总值贡献率": "contribution_rate_of_gross_domestic_product",
            "第一产业贡献率": "contribution_rate_of_the_primary_industry",
            "第二产业贡献率": "contribution_rate_of_the_secondary_industry",
            "第三产业贡献率": "contribution_rate_of_the_tertiary_industry",
        }
        res = df.groupby(["date_string", "type"]).apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "contribution_rate_of_the_three_industries")

    def run(self):
        self.run_xj()
        self.run_byj()
        self.run_zs()
        self.run_hb()
        self.run_zsgxl()
        self.run_gxl()


if __name__ == "__main__":
    GDPClean().run()
